package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.ITeamService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.competition.CompetitionApplyExamineValidate;
import com.mdd.admin.validate.team.TeamCreateValidate;
import com.mdd.admin.validate.team.TeamSearchValidate;
import com.mdd.admin.validate.team.TeamUpdateValidate;
import com.mdd.admin.vo.team.TeamDetailVo;
import com.mdd.admin.vo.team.TeamListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.team.Team;
import com.mdd.common.mapper.team.TeamMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 社团实现类
 * @author LikeAdmin
 */
@Service
public class TeamServiceImpl implements ITeamService {

    @Resource
    TeamMapper teamMapper;

    /**
     * 社团列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TeamListedVo>
     */
    @Override
    public PageResult<TeamListedVo> list(PageValidate pageValidate, TeamSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        MPJQueryWrapper<Team> queryWrapper = new MPJQueryWrapper<>();
        queryWrapper.selectAll(Team.class)
                .select("lu.nickname as ownerName")
                .innerJoin("la_user lu on lu.id = t.owner_id")
                .orderByDesc("id");

        teamMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:ownerId@owner_id:long",
            "like:orgName@org_name:str",
            "like:ownerName@lu.nickname:str",
            "=:status:long",
            "=:logo:str",
            "=:cover:str",
            "=:attachment:str",
            "=:createId@create_id:long",
            "=:updateId@update_id:long",
        });

        IPage<TeamListedVo> iPage = teamMapper.selectJoinPage(new Page<>(page, limit),TeamListedVo.class, queryWrapper);

        List<TeamListedVo> list = new LinkedList<>();
        for(TeamListedVo item : iPage.getRecords()) {
            item.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(item);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 社团详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Team
     */
    @Override
    public TeamDetailVo detail(Integer id) {
        Team model = teamMapper.selectOne(
                new QueryWrapper<Team>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TeamDetailVo vo = new TeamDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setLogo(UrlUtils.toAbsoluteUrl(model.getLogo()));
        return vo;
    }

    /**
     * 社团新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TeamCreateValidate createValidate) {
        Team model = new Team();
        model.setName(createValidate.getName());
        model.setOwnerId(createValidate.getOwnerId());
        model.setOrgName(createValidate.getOrgName());
        model.setStatus(createValidate.getStatus());
        model.setLogo(UrlUtils.toRelativeUrl(createValidate.getLogo()));
        model.setCover(createValidate.getCover());
        model.setAttachment(createValidate.getAttachment());
        model.setCreateId(createValidate.getCreateId());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateId(createValidate.getUpdateId());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        teamMapper.insert(model);
    }

    /**
     * 社团编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TeamUpdateValidate updateValidate) {
        Team model = teamMapper.selectOne(
                new QueryWrapper<Team>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setOwnerId(updateValidate.getOwnerId());
        model.setOrgName(updateValidate.getOrgName());
        model.setStatus(updateValidate.getStatus());
        model.setLogo(UrlUtils.toRelativeUrl(updateValidate.getLogo()));
        model.setCover(updateValidate.getCover());
        model.setAttachment(updateValidate.getAttachment());
        model.setCreateId(updateValidate.getCreateId());
        model.setUpdateId(updateValidate.getUpdateId());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        teamMapper.updateById(model);
    }

    /**
     * 社团删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Team model = teamMapper.selectOne(
                new QueryWrapper<Team>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        teamMapper.delete(new QueryWrapper<Team>().eq("id", id));
    }

    @Override
    public void examine(CompetitionApplyExamineValidate updateValidate) {
        Team team = new Team();
        team.setId(updateValidate.getId());
        team.setStatus(updateValidate.getStatus());
        teamMapper.updateById(team);
    }
}
